﻿"use strict";
var enterEventInput = null;
$(function () {
	$("[data-toggle='tooltip']").tooltip();
    var sensorDragDiv=null,editSensor=null;
    $(document)
    //.on('click','header .ico-edit-all',function () {
    //	$('.editable').show();
    //	$('.editableHide').hide();
    //	$('.survey_drag_box .survey_drag').addClass('drag_sure').attr('title','按住左侧悬浮图标可拖动');
    //	editWayChange();
    //})
    //.on('click','header .editAllOut',function () {
    //	$('.editable').hide();
    //	$('.editableHide').show();
    //	$('.survey_drag_box .survey_drag').removeClass('drag_sure').removeAttr('title');
    //})
    .on('click', '.shrinkBar', function() {
        $(this).parent().toggleClass('retract');
        editWayChange();
        }).on('click', '.colseFlashTip', function (e) {
            e.stopPropagation();
            $('.flashTipWrapper').css('top', '-100px');
        })
        .on('click', '.bridgeTitle', function (e) {
            e.stopPropagation();
            $('.proList').fadeToggle();
        }).on('click', '.proList', function (e) {
            e.stopPropagation();
        }).on('click', 'body', function () {
            $('.proList').hide();
            $(".area-choose-form").removeClass("openlist");
        })
        .on("click", ".base_positionremark", function (e) {
            e.stopPropagation();
            $('.area-choose-form').toggleClass("openlist")
        }).on("click", ".area-choose-list", function (e) {
            e.stopPropagation();
        }).on("click", ".area-choose-list .met1", function (e) {
            $(".area-choose-form").removeClass("openlist")
        })
    .on('click', '.ico-upx2', function () {
        setTimeout(function () {
            var nw = $("#HomeRealDataAllStasticsBody .highcharts-container").width();
            var nh = $("#HomeRealDataAllStasticsBody .highcharts-container").height();
            if (nw == 0 || nh == 0) {
                $('#HomeRealDataAllStasticsBody').highcharts().reflow();
            }
        }, 600);
    })
    .on('click', '.sideBar .sideBar_drag', function() {
        $(this).addClass('active').siblings().removeClass('active');
    })
    .on('click', '.showWay .way>.item', function() {
         if($(this).hasClass("plane")){
            $(this).parent().addClass("hideSectionList");
         }else{
            $(this).parent().removeClass("hideSectionList");
         }
        $(this).addClass('active').siblings('.item').removeClass('active');
        })
        .on('click', '.modal_3D-item', function (e) {
            if ($(this).hasClass('active')) {
                $(this).removeClass('active');
                //$('.sectionMenu a').trigger('click');
                if ($(this).hasClass("showSectionList")) {
                    $(".sectionList").removeClass("open");
                    if ($.map3DMaker) $.map3DMaker.hideSection();
                    $(".modal_3D-item").eq(2).trigger('click');
                }
            } else {
                $(this).addClass('active');
                if ($(this).hasClass("showSectionList")) {
                    $(".sectionList").addClass("open");
                    $(".modal_3D-item").eq(2).removeClass('active');
                    loadSection();
                }
            }
        })
        .on('click', '.projectInfo', function () {
           $(".projectInfoContainer").toggleClass("current");
           $(this).toggleClass('active');
        })
        .on('click', '.closeProjectInfo', function () {
            $(".projectInfo").trigger("click");
        })
    .on('click', '.showWay .editWay .switch .ico', function() {
        $(this).toggleClass('ico-switch-off ico-switch-on');
    })
    .on('click', '.tab-tit .tab-item', function() {
        $(this).addClass('active').siblings('.tab-item').removeClass('active');
        var tab = $(this).data('tab');
        if (tab == 'inspection') {
            $('#addPointAction').hide();
            if ($('#addPointAction').find('span').hasClass('actived')) {
                $('#addPointAction').find('span').trigger('click');
            };
            $.map3DMaker.closeAddPointEvent();
            $.map3DMaker.showPointType("inspectionpoint");
            $('#optionMenu').remove();
            $('#clickMenu').remove();
        } else {
            $('#addPointAction').show();
            if ($.map3DMaker != undefined) {
                $.map3DMaker.showPointType('point');
            }
        }
        $(this).parents('.tab-tit').siblings('.tab-details').find('.' + tab).show().siblings().hide();
    })
    .on('click', '.mainFloatB .head .fr', function() {
        $(this).find('.ico').toggleClass('ico-downx2 ico-upx2').parents('.head').first().siblings('.details').toggle();
        $('body').toggleClass('bendPosBottom');
        $('.mapCenter').toggleClass('current');
    })
    .on('click', '.customModal', function () {
        $('.customPanel').toggle();
    })
    .on('click', '.sideBar .addModule ', function() {
        $('.addModulePop').toggle();
    })
    .on('click', '.customPop .ico-cancel ', function() {
        $(this).parents('.customPop').first().hide();
    })
    .on('click', '.screenFilter .fr .ico ', function() {
        $(this).addClass('active').parent().siblings().find('.ico').removeClass('active');
        var sort = $(this).data('sort');
        $(this).parents('.screenFilter').siblings('.screenResult').find('.' + sort).show().siblings().hide();
    })
    .on('change', '.screenResult .table td .selectAll input[type="checkbox"]', function() {
        var selItem = $(this).parents('thead').first().siblings('tbody').find('.selectItem');

        if ($(this).prop('checked')) {
            selItem.find('input[type="checkbox"]').prop('checked', true);
        } else {
            selItem.find('input[type="checkbox"]').prop('checked', false);
        }
    })
    .on('change', '.footnote .pageSelectAll input[type="checkbox"]', function() {
        if ($(this).prop('checked')) {
            $(this).parents('.footnote').siblings('.scroll').find('input[type="checkbox"]').prop('checked', true);
        } else {
            $(this).parents('.footnote').siblings('.scroll').find('input[type="checkbox"]').prop('checked', false);
        }
    })
    .on('click', '.data_analyze .analyzeEditBtn ', function() {
        $('.data_analyze').find('.analyzeEdit').show();
        $('.data_analyze').find('.analyzeEditHide').hide();
    })
    .on('click', '.data_analyze .saveAndExit ', function() {
        $('.data_analyze').find('.analyzeEdit').hide();
        $('.data_analyze').find('.analyzeEditHide').show();

    }).on('click', '.pointSet', function (e) {
            e.stopPropagation();
            var $this_pointSetBar = $(this).parents('.ponitMapitem').find('.pointSetBar');

            $('.pointSetBar').removeClass('show');
            $this_pointSetBar.addClass('show');

        }).on('click', '.closePointSetBar', function (e) {
            e.stopPropagation();
            $(this).parents('.pointSetBar').removeClass('show')
        }).on('click', '.addPointMap', function (e) {
            var newuploadno = new Date().getTime();
            e.stopPropagation();
            var $_ponitMapitem = new Array();
            $_ponitMapitem.push('<div class="ponitMapitem">');
            $_ponitMapitem.push('<div class="ponitMapitem-top">');
            $_ponitMapitem.push('<section class="pull-right ponitFunc">');
            $_ponitMapitem.push('<a href="javascript:;" class="uploadImg">上传图片<div id="uploadImgBtn' + newuploadno +'" class="uploadImgBtn"></div></a><a href="javascript:;" class="pointSet">布点配置</a><a href="javascript:;" class="pointMapDel"></a>');
            $_ponitMapitem.push('</section>');
            $_ponitMapitem.push('<section class="ponitImgCon">');
            $_ponitMapitem.push('<input type="text" name="imgName" placeholder="布点图名称">');
            $_ponitMapitem.push('</section>');
            $_ponitMapitem.push('</div>');
            $_ponitMapitem.push('<div class="ponitMapitem-bottom clearfix">');
            $_ponitMapitem.push('<span class="pull-right">图片尺寸建议 宽480px,高240px</span>');
            $_ponitMapitem.push('<span class="pull-left pointImgName"></span>');
            $_ponitMapitem.push('<a href="javascript:;" class="viewPointImg pull-left">');
            $_ponitMapitem.push('</a>');
            $_ponitMapitem.push('</div>');
            $_ponitMapitem.push('<div class="pointSetBar">');
            $_ponitMapitem.push('<section class="pointSetBar-top">');
            $_ponitMapitem.push('<i class="pull-right ico ico-cancel-gray closePointSetBar"></i><p>&nbsp;</p>');
            $_ponitMapitem.push('</section>');
            $_ponitMapitem.push('<div class="pointNoImg">暂无图片,请上传一张图片</div>');
            $_ponitMapitem.push('</div>');
            $_ponitMapitem.push('</div>');
            $('.ponitMapList').append($_ponitMapitem.join(''));


            //绑定新按钮上传组件
            $('#uploadImgBtn' + newuploadno).uploadify({
                'buttonText': "",
                'swf': '/content/uploadify/uploadify.swf', //设置上传组件uploadify.swf的路径
                'uploader': '/FileUpload/Upload/', //设置上传的Url接口
                'cancelImg': '/content/uploadify/cancel.png',
                'removeCompleted': true,
                'hideButton': true,
                'auto': true,
                //'nowuploadno': newuploadno,
                'multi': true,
                'wmode': 'transparent',
                'uploadLimit': 99,
                'simUploadLimit': 99,
                'fileTypeDesc': '* Files (.*)',
                'onUploadStart': function (file) {
                    $("#uploadImgBtn" + newuploadno).uploadify("settings", "formData", { type: "", bridgeid: $('#BridgeId').val(), modelType: "" });
                },
                'onQueueComplete': function (queueData) { },
                'onCancel': function (file) { },
                'onUploadSuccess': function (file, resultdata, responese) {
                    var filedata = $.parseJSON(resultdata);
                    if (resultdata != '[]' && filedata != null) {
                        var url = "/" + filedata.path;
                        var filename = url.substring(url.lastIndexOf("/") + 1, url.length);
                        filename = filename.substring(0, 3) + '...' + filename.substring(filename.length - 6, filename.length);
                        var $this_parents = $('#uploadImgBtn' + newuploadno).parents('.ponitMapitem');
                        $this_parents.find('.ponitMapitem-bottom').find('.viewPointImg').html('预览<div class="pointImgCon"><img src="' + url + '" class="pointImg"></div>');
                        var $pointSetBar = new Array();
                        $pointSetBar.push('<section class="pointSetBar-top">');
                        $pointSetBar.push('<i class="pull-right ico ico-cancel-gray closePointSetBar"></i><p>用鼠标在图片上右击布点的位置，然后选择传感器</p>');
                        $pointSetBar.push('</section>');
                        $pointSetBar.push('<div class="sectionImg pointSetWrapper" oncontextmenu="javascript:return false;" data-image="' + url + '" style="background-image:url(' + url + ')">');
                        $pointSetBar.push('<section class="sectionLegend pointSelectList scroll">');
                        var pointEls = $("#monitorPoint .addpointtypepanel");
                        for (var i = 0; i < pointEls.length; i++) {
                            var pointElid = pointEls.eq(i).data('id');
                            var pointElname = pointEls.eq(i).data('name');
                            $pointSetBar.push('<span><i class="icon ico-data-' + pointElid + '" data-name="' + pointElid + '"></i>' + pointElname + '</span>');
                        };
                        //$pointSetBar.push('<span><i class="icon icon-gaocheng" data-name="gaocheng"></i>高程位移</span><span><i class="icon icon-shuiping" data-name="shuiping"></i>水平位移</span><span><i class="icon icon-shenbu"  data-name="shenbu"></i>深部位移</span>');
                        $pointSetBar.push('</section>');
                        $pointSetBar.push('</div>');

                        $this_parents.find('.pointSetBar').html($pointSetBar.join(''));
                        $this_parents.find('.sectionImg').find('.sensorPostion ').remove();
                        $this_parents.find('.pointImgName').text(filename);
                    }
                }
            });

        }).on('mousedown', '.pointSetWrapper', function (e) {
            e.stopPropagation();
            var $this_pointSelectList = $(this).find('.pointSelectList');
            if (3 == e.which) {
                var $this_pointSelectList_height = $this_pointSelectList.outerHeight(true);
                var $this_pointSelectList_width = $this_pointSelectList.outerWidth(true);
                var $this_width = $(this).width();
                var $this_height = $(this).height();
                var $this_positionX = e.pageX - $(this).offset().left; //获取当前鼠标相对img的X坐标
                var $this_positionY = e.pageY - $(this).offset().top; //获取当前鼠标相对img的Y坐标
                var $this_css_left = $this_positionX;
                var $this_css_top = $this_positionY;
                var $this_css_right = 'auto';
                var $this_css_bottom = 'auto';
                if ($this_css_left > $this_width - $this_pointSelectList_width) {
                    $this_css_left = 'auto';
                    $this_css_right = 0;
                } else {
                    $this_css_left = $this_positionX / $this_width * 100 + '%';
                };
                if ($this_css_top > $this_height - $this_pointSelectList_height) {
                    $this_css_top = "auto";
                    $this_css_bottom = 0;
                } else {
                    $this_css_top = $this_positionY / $this_height * 100 + '%';
                };

                $this_pointSelectList.css({ 'left': $this_css_left, 'top': $this_css_top, 'right': $this_css_right, 'bottom': $this_css_bottom }).unbind().on("mousedown", 'span', function (e) {
                    e.stopPropagation();
                    var $left=($this_positionX/$this_width*100)+'%';
                    var $top=($this_positionY/$this_height*100)+'%';
                    if(editSensor){
                        $left=editSensor.data('position').left;
                        $top=editSensor.data('position').top;
                        editSensor.remove();
                        editSensor=null;
                    };

                    var $this_class=$(this).find('i').attr('class');
                    var $this_name=$(this).find('i').data('name');
                    var $this_pointSelectList  =$(this).parent();
                    var $this_pointSetWrapper=$this_pointSelectList.parent();
                    $this_pointSelectList.css({'left':'-999px','right':'auto','top':'0px','bottom':'auto'});
                    var $data_position ={"left": $left,"top":$top,"name":$this_name,"id":new Date().getTime()};

                    $this_pointSetWrapper.append('<span data-position='
                        +JSON.stringify($data_position)+' class="sensorPostion '
                        +$this_class+'" style="left:'+$left+';top:'+$top+';"><section class="sensorPositionTool"><a href="javascript:;" class="editSensor">编辑</a><a href="javascript:;" class="delSensor">删除</a></section></span>');
                });
            } else {
                $this_pointSelectList.css({ 'left': '-999px', 'right': 'auto', 'top': '0px', 'bottom': 'auto' });
            };

        }).on('mousedown', '.sensorPostion', function (e) {
            e.stopPropagation();
            sensorDragDiv = $(this);
        }).on('mousemove', '.pointSetWrapper', function (e) {
            e.stopPropagation();
            if(!sensorDragDiv)return;
            var $this_pointSelectList=$(this).find('.pointSelectList');
            var $this_pointSelectList_height=$this_pointSelectList.outerHeight(true);
            var $this_pointSelectList_width=$this_pointSelectList.outerWidth(true);
            var $this_width=$(this).width();
            var $this_height=$(this).height();
            var $this_positionX=e.pageX-$(this).offset().left; //获取当前鼠标相对img的X坐标
            var $this_positionY=e.pageY-$(this).offset().top; //获取当前鼠标相对img的Y坐标

            var $this_css_left=$this_positionX;
            var $this_css_top=$this_positionY;
            var $this_css_right='auto';
            var $this_css_bottom='auto';
            if($this_css_left>$this_width-$this_pointSelectList_width){
                $this_css_left='auto';
                $this_css_right=0;
            }else{
                $this_css_left=$this_positionX/$this_width*100+'%';
            };
            if($this_css_top>$this_height-$this_pointSelectList_height){
               $this_css_top="auto"; 
               $this_css_bottom=0; 
            }else{
                $this_css_top=$this_positionY/$this_height*100+'%'; 
            };

            var $data_position = sensorDragDiv.data('position');
            $data_position.left =  $this_positionX/$this_width*100+'%';
            $data_position.top =  $this_positionY/$this_height*100+'%';
            sensorDragDiv.data('position',$data_position);
            sensorDragDiv.css({'left':$this_positionX/$this_width*100+'%','top':$this_positionY/$this_height*100+'%'});
            sensorDragDiv.find('.editSensor').data('pointselectlist',{'left':$this_css_left,'top':$this_css_top ,'right':$this_css_right,'bottom':$this_css_bottom});


        }).on('mouseup', '.pointSetWrapper', function (e) {
            e.stopPropagation();
            sensorDragDiv=null;
        }).on('mouseleave', '.pointSetWrapper', function (e) {
            e.stopPropagation();
            sensorDragDiv=null;
        }).on('click','.delSensor',function(e){
            e.stopPropagation();
            $(this).parent().parent().remove();
        }).on('click','.editSensor',function(e){
            var $this_parents=$(this).parents('.pointSetWrapper');
            var $this_pointSelectList=$this_parents.find('.pointSelectList');
            var $this_pointSelectList_height=$this_pointSelectList.outerHeight(true);
            var $this_pointSelectList_width=$this_pointSelectList.outerWidth(true);

            var $this_width=$this_parents.width();
            var $this_height=$this_parents.height();
            var $this_positionX=$(this).parents('.sensorPostion').position().left; //获取当前鼠标相对img的X坐标
            var $this_positionY=$(this).parents('.sensorPostion').position().top; //获取当前鼠标相对img的Y坐标
            
            console.log($this_positionX+'--'+$this_positionY);

            var $this_css_left=$this_positionX;
            var $this_css_top=$this_positionY;
            var $this_css_right='auto';
            var $this_css_bottom='auto';
            if($this_css_left>$this_width-$this_pointSelectList_width){
                $this_css_left='auto';
                $this_css_right=0;
            }else{
                $this_css_left=$this_positionX/$this_width*100+'%';
            };
            if($this_css_top>$this_height-$this_pointSelectList_height){
               $this_css_top="auto"; 
               $this_css_bottom=0; 
            }else{
                $this_css_top=$this_positionY/$this_height*100+'%'; 
            };

            $this_pointSelectList.css({'left':$this_css_left,'top':$this_css_top ,'right':$this_css_right,'bottom':$this_css_bottom});
            
            editSensor = $(this).parent().parent();

        }).on('click', '.pointMapDel', function (e) {
            e.stopPropagation();
            if ($(".ponitMapitem").length > 1) {
                $(this).parents('.ponitMapitem').remove();
            } else {
                layer.msg('无法删除最后一条！')
            }
        });
    $('#sectionModal').on('shown.bs.modal', function () {
        // 执行一些动作...
        $('#uploadImgBtn').uploadify({
            'buttonText': "",
            'swf': '/content/uploadify/uploadify.swf', //设置上传组件uploadify.swf的路径
            'uploader': '/FileUpload/Upload/', //设置上传的Url接口
            'cancelImg': '/content/uploadify/cancel.png',
            'removeCompleted': true,
            'hideButton': true,
            'auto': true,
            //'nowuploadno': newuploadno,
            'multi': true,
            'wmode': 'transparent',
            'uploadLimit': 99,
            'simUploadLimit': 99,
            'fileTypeDesc': '* Files (.*)',
            'onUploadStart': function (file) {
                $("#uploadImgBtn").uploadify("settings", "formData", { type: "", bridgeid: $('#BridgeId').val(), modelType: "" });
            },
            'onQueueComplete': function (queueData) { },
            'onCancel': function (file) { },
            'onUploadSuccess': function (file, resultdata, responese) {

                var filedata = $.parseJSON(resultdata);
         
                if (resultdata != '[]' && filedata != null) {
                    var url = "/" + filedata.path;
                    var filename = url.substring(url.lastIndexOf("/") + 1, url.length);
                    filename = filename.substring(0, 3) + '...' + filename.substring(filename.length - 6, filename.length);
                    var $this_parents = $('#uploadImgBtn').parents('.ponitMapitem');
                    $this_parents.find('.ponitMapitem-bottom').find('.viewPointImg').html('预览<div class="pointImgCon"><img src="' + url + '" class="pointImg"></div>');
                    var $pointSetBar = new Array();
                    $pointSetBar.push('<section class="pointSetBar-top">');
                    $pointSetBar.push('<i class="pull-right ico ico-cancel-gray closePointSetBar"></i><p>用鼠标在图片上右击布点的位置，然后选择传感器</p>');
                    $pointSetBar.push('</section>');
                    $pointSetBar.push('<div class="sectionImg pointSetWrapper" oncontextmenu="javascript:return false;" data-image="' + url + '" style="background-image:url(' + url + ')">');
                    $pointSetBar.push('<section class="sectionLegend pointSelectList scroll">');
                    var pointEls = $("#monitorPoint .addpointtypepanel");
                    for (var i = 0; i < pointEls.length; i++) {
                           var pointElid = pointEls.eq(i).data('id');
                           var pointElname = pointEls.eq(i).data('name');
                           $pointSetBar.push('<span><i class="icon ico-data-' + pointElid + '" data-name="' + pointElid+'"></i>' + pointElname + '</span>');
                    };
                   // $pointSetBar.push('<span><i class="icon icon-gaocheng" data-name="gaocheng"></i>高程位移</span><span><i class="icon icon-shuiping" data-name="shuiping"></i>水平位移</span><span><i class="icon icon-shenbu"  data-name="shenbu"></i>深部位移</span>');
                    $pointSetBar.push('</section>');
                    $pointSetBar.push('</div>');

                    $this_parents.find('.pointSetBar').html($pointSetBar.join(''));
                    $this_parents.find('.sectionImg').find('.sensorPostion ').remove();
                    $this_parents.find('.pointImgName').text(filename);

                }
            }
        });
    })
	function editWayChange () {
		var w = parseInt($('.editWay').width());
		if (w <= 890) {
		    $('.editWay > .fr .text').hide().siblings('.ico').css('margin-right', '0');
		} else {
		    $('.editWay > .fr .text').show().siblings('.ico').css('margin-right', '10px');
        }
        $('#HomeRealDataAllStasticsBody').highcharts().reflow();

    }
    $(".dragdialogwindow .close").on('click',
        function () {
            $(this).parent().parent().parent().parent().removeClass("in").hide();
        });

    document.onkeydown = function (e) {
        if (!e) {
            e = window.event;
        }
        if ((e.keyCode || e.which) === 13) {
            if (enterEventInput != null) {
                $(enterEventInput).hide();
            }
        }
    }

    $(".situation").on("click", '.normal', function (e) {
        var yichangNum = parseInt($(this).find(".warningcount").text());
        if (yichangNum === 0)
            return;
        e.stopPropagation();
        $('.warningBar').show();
        LoadMapModelShowPointFromWarningWindow();
    });
    $('#sensorDataModal').on('hidden.bs.modal', function (e) {
        if (window.windowRealDataSocket != null) {
            window.windowRealDataSocket.close();
        }
    });
});
